package com.example.bluetooth;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;

import com.example.webviewtest.R;

import java.util.Timer;
import java.util.TimerTask;

public class MyBlueToothService extends Service {
    public BLEPositioning blePositioning;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //app启动时开启蓝牙扫描功能，上传蓝牙数据至5g运营平台
//         blePositioning = new BLEPositioning(this);
        blePositioning = new BLEPositioning(getApplication());
        Timer timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    blePositioning.stopScan();
                    blePositioning.startScan();
                }catch (Exception e) {
                    System.out.println(e);
                    timer1.cancel();
                }
            }
        },0,10000);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        blePositioning.stopScan();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
//        createNotificationChannel();
        Intent notificationIntent = new Intent(this, getApplication().getClass());
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, "CHANNEL_ID")
                .setContentTitle("正在获取蓝牙信息")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1000, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;
    }
}
